﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.CvEnum;


namespace DemoAdaboost
{
    public partial class MultipleCameraCapture : Form
    {
        private Capture capture;
        //private bool captureInProgress;
        private HaarCascade haar;            //the viola-jones classifier (detector)
        //private CascadeClassifier haar;

        public MultipleCameraCapture()
        {
            InitializeComponent();

            //haar = new HaarCascade("haarcascade_frontalface_default.xml");
            //haar = new CascadeClassifier("haarcascade_frontalface_default.xml");
        }

        private void ProcessFrame(object sender, EventArgs arg)
        {
            //Read an image from hard disk at location e.g ‘E:\IMAGES\’
            //Image InputImg = Image.FromFile(@"E:\lena.jpg");
            //Image<Bgr, byte> ImageFrame = new Image<Bgr, byte>(new Bitmap(InputImg));

            

            //fetch the frame captured by web camera
            Image<Bgr, Byte> ImageFrame = capture.QueryFrame();

            //Detect Faces from the ImageFrame
            if (ImageFrame != null)   // confirm that image is valid
            {
                //convert the image to gray scale
                Image<Gray, byte> grayframe = ImageFrame.Convert<Gray, byte>();

                //detect faces from the gray-scale image and store into an array of type 'var',i.e 'MCvAvgComp[]'
                var faces = grayframe.DetectHaarCascade(haar, 1.4, 4,
                                        HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                        new Size(25, 25))[0];

                //draw a green rectangle on each detected face in image
                foreach (var face in faces)
                {
                    ImageFrame.Draw(face.rect, new Bgr(Color.Green), 3);
                }
            }

            CamImageBox.Image = ImageFrame;
        }

        private void cbCamIndex_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Set the camera number to the one selected via combo box
            int CamNumber = 0;
            CamNumber = int.Parse(cbCamIndex.Text);

            //Start the selected camera
            #region if capture is not created, create it now
            if (capture == null)
            {
                try
                {
                    capture = new Capture(CamNumber);
                }
                catch (NullReferenceException excpt)
                {
                    MessageBox.Show(excpt.Message);
                }
            }
            #endregion

            //Start showing the stream from camera
            btnStart_Click(sender, e); 
            btnStart.Enabled = true;   //enable the button for pause/resume
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (capture != null)
            {
                if (btnStart.Text == "Pause")
                {  //if camera is getting frames then pause the capture and set button Text to
                    // "Resume" for resuming capture
                    btnStart.Text = "Resume"; //
                    Application.Idle -= ProcessFrame;
                }
                else
                {
                    //if camera is NOT getting frames then start the capture and set button
                    // Text to "Pause" for pausing capture
                    btnStart.Text = "Pause";
                    Application.Idle += ProcessFrame;
                }

            }
        }

        private void MultipleCameraCapture_Load(object sender, EventArgs e)
        {
            // adjust path to find your xml at loading
            haar = new HaarCascade("haarcascade_frontalface_default.xml");
            //haar = new HaarCascade("haarcascade_eye.xml");  //detected eyes
       }

        /// <summary>
        /// Disconnects from the camera
        /// </summary>
        private void ReleaseData()
        {
            if (capture != null)
                capture.Dispose();
        }
    }
}
